package com.yilin.medical.adapter;

import java.util.List;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yilin.medical.R;
import com.yilin.medical.model.MessageBean;
import com.yilin.medical.utils.ToastUntil;
import com.yilin.medical.views.SlidingMenu;

/**
 * 肿瘤adapter
 * 
 * @author lijianqun
 * 
 */
public class MessageAdapter extends BaseAdapter {
	private Context context;
	private List<MessageBean> list;
	private int mScreenWidth;
	ViewHolder holder = null;

	public MessageAdapter(Context context, List<MessageBean> list) {
		super();
		this.context = context;
		this.list = list;
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		mScreenWidth = outMetrics.widthPixels;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final MessageBean bean;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View
					.inflate(context, R.layout.listitem_message, null);
			holder.root = (SlidingMenu) convertView.findViewById(R.id.root);
			holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
			holder.tv_delete = (TextView) convertView
					.findViewById(R.id.tv_delete);
			holder.rl_showpart = (RelativeLayout) convertView
					.findViewById(R.id.rl_showpart);
			holder.rl_huifu = (RelativeLayout) convertView
					.findViewById(R.id.rl_huifu);
			holder.rl_fabupinglun = (RelativeLayout) convertView
					.findViewById(R.id.rl_fabupinglun);
			holder.ll_fabupinglun = (LinearLayout) convertView
					.findViewById(R.id.ll_fabupinglun);
			holder.et_content = (EditText) convertView
					.findViewById(R.id.et_content);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		bean = list.get(position);
		if (bean.isShow) {
			holder.root.scrollTo(0, 0);
			holder.root.setFlag(true);
			holder.rl_huifu.setVisibility(View.GONE);
			holder.ll_fabupinglun.setVisibility(View.VISIBLE);
			holder.et_content.requestFocus();
			holder.et_content.setFocusable(true);
			holder.et_content.setFocusableInTouchMode(true);
			
		} else {
			holder.root.setFlag(false);
			holder.root.scrollTo(0, 0);
			holder.rl_huifu.setVisibility(View.VISIBLE);
			holder.ll_fabupinglun.setVisibility(View.GONE);

		}
		holder.root.scrollTo(0, 0);
		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.rl_showpart
				.getLayoutParams();
		params.width = mScreenWidth;
		holder.rl_showpart.setLayoutParams(params);
		holder.ll_fabupinglun.setLayoutParams(params);
		holder.tv_delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ToastUntil.showShortToast(context, "删除当前条目");
			}
		});
		holder.rl_huifu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				bean.isShow = true;
				notifyDataSetChanged();
			}
		});
		holder.rl_fabupinglun.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				bean.isShow = false;
				holder.root.setFlag(false);
				notifyDataSetChanged();
				ToastUntil.showShortToast(context, "回复成功");
			}
		});
		return convertView;
	}

	class ViewHolder {
		SlidingMenu root;
		TextView tv_num, tv_delete;
		RelativeLayout rl_showpart, rl_huifu, rl_fabupinglun;
		LinearLayout ll_fabupinglun;
		EditText et_content;
	}

}
